package eos.dom.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector;

public class DomClient {

	public final static String url = "http://10.0.2.2:8888/dominionappserver";
	protected HttpURLConnection server;
	private String name;
	private DomInterface gui;
	private String game;

	public String getGame() {
		return game;
	}

	public void setGame(String game) {
		this.game = game;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public DomClient(String name,DomInterface gui) throws Exception{
		this.gui = gui;
		this.name = name;
	}

	public boolean createGame() throws Exception{
		connect("POST");
		post("command=CREATE_GAME&name="+name);
		String s = ""+getResponse();
		disconnect();
		return s.startsWith("OK");
	}

	public void endGame() throws Exception{
		connect("POST");
		post("command=END_GAME&name="+game);
		displayResponse();
		disconnect();
	}

	/**
	 * @param method: String object for client method (POST, GET,...)
	 */
	public void connect(String method) throws Exception
	{
		try
		{
			URL u = new URL(url);
			server = (HttpURLConnection)u.openConnection();
			server.setDoInput(true);
			server.setDoOutput(true);
			server.setRequestMethod(method);
			server.connect();
		}
		catch (Exception e)
		{
			throw new Exception("Connection failed\n"+e.getMessage()+"\n"+server.getURL().toString());
		}
	}

	public void disconnect()
	{
		server.disconnect();
	}

	public void displayResponse() throws Exception
	{
		String line;

		try
		{
			BufferedReader s = new BufferedReader(
					new InputStreamReader(
							server.getInputStream()));
			line = s.readLine();
			while (line != null)
			{
				System.out.println(line);
				line = s.readLine();
			}
			s.close();
		}
		catch(Exception e)
		{
			throw new Exception("Unable to read input stream");
		}
	}

	public Vector<String> receiveGameList() throws Exception{
		Vector<String> list = new Vector<String>();
		String line;
		connect("POST");
		post("command=GET_GAME_LIST");
		try
		{
			BufferedReader s = new BufferedReader(
					new InputStreamReader(
							server.getInputStream()));
			line = s.readLine();
			while (line != null)
			{
				list.add(line);
				line = s.readLine();
			}
			s.close();
			disconnect();
		}

		catch(Exception e)
		{
			throw new Exception("Unable to read input stream");
		}
		return list;
	}

	public void post(String s) throws Exception
	{
		try
		{
			BufferedWriter bw = new BufferedWriter(
					new OutputStreamWriter(
							server.getOutputStream()));
			bw.write(s, 0, s.length());
			bw.flush();
			bw.close();
		}
		catch(Exception e)
		{
			throw new Exception("Unable to write to output stream");
		}
	}


	public boolean joinGame(String host) throws Exception {
		connect("POST");
		post("command=JOIN_GAME&name="+name+"&host="+host);
		String s = getResponse();
		disconnect();
		return s.startsWith("OK");
	}

	private String getResponse() throws Exception {
		String line="";
		try
		{
			BufferedReader s = new BufferedReader(
					new InputStreamReader(
							server.getInputStream()));
			line = s.readLine();
			String stop = line;
			while (stop != null)
			{
				stop = s.readLine();
				line +="\n"+ stop;
			}
			s.close();
			return line;
		}
		catch(Exception e)
		{
			throw new Exception("Unable to read input stream");
		}
	}

	public void start() {
		// TODO Auto-generated method stub

	}

	public void prepareGame(int[] cardsChosen) throws Exception {
		connect("POST");
		String post = "command=PREPARE_GAME&name="+name+"&nbCards="+cardsChosen.length;
		for(int i = 0; i<cardsChosen.length;i++){
			post+="&card"+i+"="+cardsChosen[i];
		}
		post(post);
		String s = getResponse();
		disconnect();
	}

	public String getUpdateInfo() throws Exception {
		connect("POST");
		String post = "command=GET_UPDATE&name="+name+"&host="+game;
		post(post);
		String s = getResponse();
		disconnect();
		return s;
	}


}